import { location } from "../utils/getLocation";
import { guiMover } from "../utils/gui";
import MiniGamesSettings from "../utils/MiniGamesSettings";
import { DrawString, ServerTick, timedisplay } from "../utils/utils";

export function skywarsFeatures() {}

//self attack sword
register("attackEntity" , (entity,event) => { 
    if(location.game != "SKYWARS" || !location.mode.includes("insane_lucky")) return;
    if(!MiniGamesSettings.AntiSuicide) return;
    if(
        Player.getHeldItem() && Player.getHeldItem().getName().removeFormatting() == "Self-Attacking Sword"
    ) {
        cancel(event);
        World.playSound("random.anvil_land",1,1);
    }
})

//The Devil's Contract
let DevilsContractTimer = 0;
const DevilsContract = register("tick", () => {
    if(location.game != "SKYWARS" || !location.mode.includes("insane_lucky")) return;
    if(!MiniGamesSettings.FastDevilsContract) return;
    if (
        Player.getHeldItem() && Player.getHeldItem().getName().removeFormatting() == "The Devil's Contract"
    ) {
        setTimeout(() => {
            if(Player.getHeldItem() && Player.getHeldItem().getName().removeFormatting() == "The Devil's Contract") 
            ChatLib.command("devilscontract accept");
            if(DevilsContractTimer < ServerTick.getTime()) DevilsContractTimer = ServerTick.getTime() + 60 * 1000;
        }, 200);
        DevilsContract.unregister();
        setTimeout(() => {
            DevilsContract.register();
        }, 500);
    }
})

register("worldLoad", () => {
    DevilsContractTimer = 0;
})

register("renderOverlay", () => {
    if(!MiniGamesSettings.FastDevilsContract) return;
    if(guiMover.gui.isOpen()) {
        DrawString("§c恶魔契约§7: §e" + timedisplay(56789),guiMover.settings.DevilsContract.x,guiMover.settings.DevilsContract.y,guiMover.settings.DevilsContract.s,true);
        return;
    }
    if(location.game != "SKYWARS" || !location.mode.includes("insane_lucky")) return;
    if(DevilsContractTimer < ServerTick.getTime()) return;
    DrawString("§c恶魔契约§7: §e" + timedisplay(DevilsContractTimer - ServerTick.getTime()),guiMover.settings.DevilsContract.x,guiMover.settings.DevilsContract.y,guiMover.settings.DevilsContract.s,true);
})

//Stinged Leggings
register("spawnParticle", (particle,type,event) => {
    if(location.game != "SKYWARS" || !location.mode.includes("insane_lucky")) return;
    if(!MiniGamesSettings.StingedLeggings) return;
    let leg = Player.armor.getLeggings();
    if(!leg || leg.getName().removeFormatting() != "Stinged Leggings") return;
    if(particle.distanceTo(Player.getPlayer()) > 5) return;
    if(type.toString() == "SPELL_WITCH") cancel(event);
})

//Aspect of the End 计数
let AspectoftheEndCount = 0;
register("playerInteract", (action,pos,event) => {
    if(location.game != "SKYWARS" || !location.mode.includes("insane_lucky")) return;
    if(!action.toString().includes("RIGHT_CLICK")) return;
    let item = Player.getHeldItem();
    if(!item || item.getName().removeFormatting() != "Aspect of the End") return;
    AspectoftheEndCount++;
})

register("worldLoad", () => {
    AspectoftheEndCount = 0;
})

register("renderOverlay", () => {
    if(!MiniGamesSettings.AspectOfTheEndCount) return;
    if(guiMover.gui.isOpen()) {
        DrawString("§bAOTE§7: §e5",guiMover.settings.SkywarsAOTECount.x,guiMover.settings.SkywarsAOTECount.y,guiMover.settings.SkywarsAOTECount.s,true);
        return;
    }
    if(location.game != "SKYWARS" || !location.mode.includes("insane_lucky")) return;
    if(AspectoftheEndCount < 1 || Player.getInventory().getItems().filter(e => e && e.getName().removeFormatting() == "Aspect of the End").length == 0) return;
    DrawString("§bAOTE§7: §e" + AspectoftheEndCount,guiMover.settings.SkywarsAOTECount.x,guiMover.settings.SkywarsAOTECount.y,guiMover.settings.SkywarsAOTECount.s,true);
})

//假设我有一个指南针
const Compass = new Item("minecraft:compass");
register("renderOverlay", () => {
    if(MiniGamesSettings.MaybeIhaveCompass == 0) return;
    if(guiMover.gui.isOpen()) {
        Compass.draw(guiMover.settings.Compass.x,guiMover.settings.Compass.y,guiMover.settings.Compass.s);
    }
    if(!((location.game == "SKYWARS" && location.mode.includes("insane_lucky")) || (location.game == "BEDWARS" && location.mode != ""))) return;
    if(MiniGamesSettings.MaybeIhaveCompass == 1 && Player.getInventory().getItems().filter(e => e && e.getID() == 345).length == 0) return;
    Compass.draw(guiMover.settings.Compass.x,guiMover.settings.Compass.y,guiMover.settings.Compass.s);
})